package com.sina.shop.manage.config;

import com.sina.shop.domain.Status;
import com.sina.shop.domain.admin.Admin;
import com.sina.shop.domain.admin.Permission;
import com.sina.shop.domain.admin.Role;
import com.sina.shop.manage.service.admin.AdminManageService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-10-30-14:43
 */
public class ShiroRealm extends AuthorizingRealm {

    protected final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @Autowired
    @Qualifier("adminManageService")
    private AdminManageService adminManageService;


    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        LOG.info("doGetAuthorizationInfo:{}", principals);
        String account = (String) getAvailablePrincipal(principals);
        Admin admin = adminManageService.getAdmin(account);
        Set<String> pers = new HashSet<>();
        Set<String> rols = new HashSet<>();
        Set<Role> roles = admin.getRoles();
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                Set<Permission> permissions = role.getPermissions();
                if (permissions != null && permissions.size() > 0) {
                    for (Permission permission : permissions) {
                        pers.add(permission.getName());
                    }
                }
                rols.add(role.getName());
            }
        }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(pers);//将权限放入shiro中。
        info.setRoles(rols);//将角色放入到shiro中。
        return info;
    }

    //认证、登录
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        LOG.info("doGetAuthenticationInfo:{}", token);
        UsernamePasswordToken uToken = (UsernamePasswordToken) token;//获取用户输入的token
        String name = uToken.getUsername();
        Admin admin = adminManageService.getAdmin(name);
        if (admin == null) {
            throw new UnknownAccountException();// 没找到帐号
        }
        if (Status.NOT_USE.equals(admin.getStatus())) {
            throw new LockedAccountException(); // 帐号锁定
        }
       /* Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("username", admin.getName());*/
        return new SimpleAuthenticationInfo(admin.getAccount(), admin.getPassword(), this.getClass().getName());//放入shiro.调用CredentialsMatcher检验密码
    }
}
